﻿'/////////////////////////////////////////////////////////
'/// LibTextBoxRegExp
'/// Componente para validação de textos
'/// Utilize quando for necessário validar campos com 
'/// formato espeficido, como email, cnpj, cpf, CEP, etc..
'/////////////////////////////////////////////////////////

Imports System.Drawing
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Public Class LibTextBoxRegExp
    Inherits MaskedTextBox

    Public Enum ValidState
        Normal = 1
        Valid = 2
        Invalid = 3
    End Enum

    '/////////////////////////////////////////
    '/// Private Members
    '/////////////////////////////////////////
    Private _Rule As String = "^.*$"            '/// Utilize expressoes regulares, ex: ^[0-9]$
    Private _ValidateOnLeave As Boolean         '/// Validar ao deixar o Componente
    Private _ValidateOnChange As Boolean        '/// Validar ao mudar o valor do Componente
    Private _Required As Boolean = True         '/// É um campo requerido
    Private _IsValid As Boolean                 '/// Usado para validação em formulário, sempre está com o valor da ultima validação

    Private _ShowErrorColor As Boolean = True   '/// Mostrar coloração quando ocorrer um erro
    Private _ShowValidColor As Boolean = True   '/// Mostrar coloração quando for válido

    Private _ErrorColor As Color = Color.OrangeRed  '/// Cor quando for inválido
    Private _NormalColor As Color = Color.White     '/// Cor normal do Componente
    Private _ValidColor As Color = Color.LightGreen '/// Cor quando for válido

    '/////////////////////////////////////////
    '/// Properties
    '/////////////////////////////////////////
    Property Rule() As String
        Get
            Return _Rule
        End Get
        Set(ByVal value As String)
            _Rule = value
        End Set
    End Property

    Property ValidateOnLeave() As Boolean
        Get
            Return _ValidateOnLeave
        End Get
        Set(ByVal value As Boolean)
            _ValidateOnLeave = value
        End Set
    End Property

    Property ValidateOnChange() As Boolean
        Get
            Return _ValidateOnChange
        End Get
        Set(ByVal value As Boolean)
            _ValidateOnChange = value
        End Set
    End Property

    Property Required() As Boolean
        Get
            Return _Required
        End Get
        Set(ByVal value As Boolean)
            _Required = value
        End Set
    End Property

    Property IsValid() As Boolean
        Get
            Return _IsValid
        End Get
        Set(ByVal value As Boolean)
            _IsValid = value
        End Set
    End Property

    Property ShowErrorColor() As Boolean
        Get
            Return _ShowErrorColor
        End Get
        Set(ByVal value As Boolean)
            _ShowErrorColor = value
        End Set
    End Property

    Property ShowValidColor() As Boolean
        Get
            Return _ShowValidColor
        End Get
        Set(ByVal value As Boolean)
            _ShowValidColor = value
        End Set
    End Property

    Property ErrorColor() As Color
        Get
            Return _ErrorColor
        End Get
        Set(ByVal value As Color)
            _ErrorColor = value
        End Set
    End Property

    Property NormalColor() As Color
        Get
            Return _NormalColor
        End Get
        Set(ByVal value As Color)
            _NormalColor = value
        End Set
    End Property

    Property ValidColor() As Color
        Get
            Return _ValidColor
        End Get
        Set(ByVal value As Color)
            _ValidColor = value
        End Set
    End Property

    '/////////////////////////////////////////
    '/// Events
    '/////////////////////////////////////////
    Public Event OnValidChange(ByVal sender As Object, ByVal e As EventArgs, ByVal state As ValidState)

    '/////////////////////////////////////////
    '/// Private Methods
    '/////////////////////////////////////////
    Private Function IsValidText() As ValidState
        'Dim ismatch As Boolean
        Dim state As ValidState
        If Me.Text <> String.Empty And Me._Rule <> String.Empty Then
            If Regex.IsMatch(Me.Text, Me._Rule) Then
                state = ValidState.Valid
            Else
                state = ValidState.Invalid
            End If
        ElseIf Me.Text = String.Empty Then
            If _Required Then
                state = ValidState.Invalid
            Else
                state = ValidState.Normal
            End If
        Else
            state = ValidState.Normal
        End If

        If state = ValidState.Valid Then
            _IsValid = True
        Else
            _IsValid = False
        End If

        RaiseEvent OnValidChange(Me, New EventArgs, state)
        Return state
    End Function

    '/////////////////////////////////////////
    '/// Protected Events
    '/////////////////////////////////////////
    Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
        If _ValidateOnLeave Then
            Dim state As ValidState
            state = IsValidText()
            If state = ValidState.Valid Then
                Me.BackColor = Me._NormalColor

            ElseIf state = ValidState.Invalid Then
                If _ShowErrorColor Then
                    Me.BackColor = Me._ErrorColor
                Else
                    Me.BackColor = Me._NormalColor
                End If
            Else
                Me.BackColor = Me.NormalColor
            End If
        Else
            Me.BackColor = Me._NormalColor
        End If
        MyBase.OnLeave(e)
    End Sub

    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        If _ValidateOnChange Then
            Dim state As ValidState
            state = IsValidText()
            If state = ValidState.Valid Then
                If _ShowValidColor Then
                    Me.BackColor = Me._ValidColor
                Else
                    Me.BackColor = Me._NormalColor
                End If
            ElseIf state = ValidState.Invalid Then
                If _ShowErrorColor Then
                    Me.BackColor = Me._ErrorColor
                Else
                    Me.BackColor = Me._NormalColor
                End If
            Else
                Me.BackColor = Me.NormalColor
            End If
        End If
        MyBase.OnTextChanged(e)
    End Sub
End Class
